<?php

/**
 * This is the model class for table "shift".
 *
 * The followings are the available columns in table 'shift':
 * @property integer $idshift
 * @property string $name
 * @property string $start_time
 * @property string $end_time
 * @property string $description
 * @property string $enabled
 * @property string $availability
 * @property string $changes_allowed
 * @property integer $team_id
 * @property string $require_acct_approval
 *
 * The followings are the available model relations:
 * @property Schedule[] $schedules
 * @property Team $team
 * @property ShiftChange[] $shiftChanges
 * @property ShiftChange[] $shiftChanges1
 */
class Shift extends CActiveRecord
{
	public static $yesNoValues= array('0'=>'No', '1'=>'Yes');

	 public static function getYesNoValue($bin=null){
	 
		if($bin!==null)
			return self::$yesNoValues[$bin];
		return self::$yesNoValues;
	 }


	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Shift the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'shift';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name, start_time, end_time, team_id', 'required'),
			array('team_id', 'numerical', 'integerOnly'=>true),
			array('name', 'length', 'max'=>45),
			array('description', 'length', 'max'=>100),
			array('enabled, availability, changes_allowed, require_acct_approval', 'length', 'max'=>1),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('idshift, name, start_time, end_time, description, enabled, availability, changes_allowed, team_id, require_acct_approval', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'schedules' => array(self::HAS_MANY, 'Schedule', 'idshift'),
			'team_relation' => array(self::BELONGS_TO, 'Team', 'team_id'),
			'shiftChanges' => array(self::HAS_MANY, 'ShiftChange', 'current_shift'),
			'shiftChanges1' => array(self::HAS_MANY, 'ShiftChange', 'requested_shift'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'idshift' => 'Id',
			'name' => 'Shift Name',
			'start_time' => 'Start Time',
			'end_time' => 'End Time',
			'description' => 'Description',
			'enabled' => 'Enabled',
			'availability' => 'Availability',
			'changes_allowed' => 'Changes Allowed',
			'team_id' => 'Team',
			'require_acct_approval' => 'Requires Accounting Approval',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search($team_id=null)
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		if($team_id != null)
			$criteria->condition = 'team_id = '.$team_id;			
		
		$criteria->compare('idshift',$this->idshift);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('start_time',$this->start_time,true);
		$criteria->compare('end_time',$this->end_time,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('enabled',$this->enabled,true);
		$criteria->compare('availability',$this->availability,true);
		$criteria->compare('changes_allowed',$this->changes_allowed,true);
		$criteria->compare('team_id',$this->team_id);
		$criteria->compare('require_acct_approval',$this->require_acct_approval,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	
}